﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc nhận được message từ client trong lượt Battle phase
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using ServerYuGi.Converter;
using ServerYuGi.Helper;
using ManagementEffect;

namespace ServerYuGi
{
    public partial class SSupervisor 
    {
        void Process_CARD_TANCONG(params object[] param)
        {

            try
            {
                int IDAttack = (int)param[0];
                int IDDefense = (int)param[1];
                bool check = false;

                YMSG TrangThaiPhongThu = (YMSG)param[2];
                SPlayer playerPhongThu = LayPlayerKhacVoiPlayerDangTrongLuot();
                SPlayer playerTanCong = LayPlayer(TurnManager.IDPlayerTrongLuot);

                SCard CardAttack = playerTanCong.LayCardTrenBan(IDAttack);
                SCard CardDefense = playerPhongThu.LayCardTrenBan(IDDefense);


                YMonsterCard CardTanCong = CardAttack.MyYBaseCard as YMonsterCard;
                YMonsterCard CardPhongThu = CardDefense.MyYBaseCard as YMonsterCard;
                
                //-------------------B1: Check Block--------------------------------//
                 check = BattleHelper.CheckBlock_TanCong(playerTanCong, IDAttack);
                //--------------------------------------------------------------//
                if (check == false)
                     return;

                /*------------------B2: Check la effect Tan Cong------------------------*/                
                ExEffectManager.IDPlayerCheck = playerTanCong.ID;
                ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_ATTACK;
                ExEffectManager.Init();
                RevMSGmanager.isPlayerKoTrongLuot = true;
                List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_ATTACK };
                check = Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,bi_tan_cong_id,tan_cong_id", api_manager, IDDefense, IDAttack);
                RevMSGmanager.Init();
                //----------------------------------------------------------//

                 if (check == false)
                     return;
               
                //------------------B3: Xoa Block la vua tan cong-----------------------//
                if(playerTanCong.Block.DuocPhep_DoiTrangThai.Count > 0)
                    playerTanCong.Block.DuocPhep_DoiTrangThai.Remove(IDAttack);
                playerTanCong.Block.DuocPhep_TanCong.Remove(IDAttack);
                //---------------------------------------------------------------------//

                //-----------------B4: Buoc tan cong co ban---------------------------//
                BattleHelper.Attack(check,this, IDAttack, IDDefense, TrangThaiPhongThu, ref playerTanCong, ref playerPhongThu, 
                    CardAttack, CardDefense);
                //--------------------------------------------------------------------//


                /*------------------B5: Check CheckPoint sau khi tan cong xong------------------------*/
                ExEffectManager.IDPlayerCheck = playerTanCong.ID;
                ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_AFTER_ATTACK;
                ExEffectManager.Init();
                RevMSGmanager.isPlayerKoTrongLuot = true;
                checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_AFTER_ATTACK };
                check = Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,tan_cong_id", api_manager, IDAttack);
                RevMSGmanager.Init();
                //----------------------------------------------------------//                



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        void Process_CARD_TANCONG_TRUCTIEP(params object[] param)
        {
            SPlayer player = LayPlayer(TurnManager.IDPlayerTrongLuot);
            SPlayer Other = LayPlayerKhacVoiPlayerDangTrongLuot();
            SCard cardTanCong = player.LayCardTrenBan((int)param[0]);
            bool check = false;

            //-------------------B1: Check Block--------------------------------//
            check = BattleHelper.CheckBlock_TanCongTrucTiep(player);
            //--------------------------------------------------------------//
            if (check == false)
                return;

            /*------------------B2: Check la effect Tan Cong Truc Tiep------------------------*/
            ExEffectManager.IDPlayerCheck = player.ID;
            ExEffectManager.Init();
            ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_DIRECTATTACK;
            RevMSGmanager.isPlayerKoTrongLuot = true;
            ExEffectManager.SetName("api_manager","tancong_id");
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_DIRECTATTACK};
            check = Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,tan_cong_id", api_manager, cardTanCong.MyYBaseCard.ID);
            RevMSGmanager.Init();
            /*-------------------------------------------------*/
            if (check == false)
                return;

            //------------------B3: Xoa Block la vua tan cong-----------------------//
            if (player.Block.DuocPhep_DoiTrangThai.Count > 0)
                player.Block.DuocPhep_DoiTrangThai.Remove(cardTanCong.MyYBaseCard.ID);
            player.Block.DuocPhep_TanCong.Remove(cardTanCong.MyYBaseCard.ID);
            //-----------------------------------------------------------------------//

            //-----------------B4: Tan Cong Truc Tiep Co Ban-------------------------//
            BattleHelper.DirectAttack(check,this, cardTanCong, ref player, ref Other);
            //-----------------------------------------------------------------------//


            ExEffectManager.IDPlayerCheck = player.ID;
            ExEffectManager.Init();
            ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_AFTER_DIRECTATTACK;
            RevMSGmanager.isPlayerKoTrongLuot = true;            
            checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_AFTER_DIRECTATTACK};
            check = Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,tan_cong_id", api_manager, cardTanCong.MyYBaseCard.ID);
            RevMSGmanager.Init();
        }

       
    }
}
